package cat20032010;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author aDeeY
 */
public class Waiter extends javax.swing.JPanel {
    Vector data = new Vector();
    Vector columnNames = new Vector();
    Statement stmt;
    ResultSet rset;
    Boolean clicked = false;

    //static AddOrder AddOrder = new AddOrder();

    public Waiter() {
        initComponents();
        try {
            TableDetail();
        } catch (SQLException ex) {
            Logger.getLogger(Waiter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jcbTable = new javax.swing.JComboBox();
        jbtnAddOrder = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtList = new javax.swing.JTable();
        jbtnDeleteOrder = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setLayout(null);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Order", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 24))); // NOI18N
        jPanel2.setLayout(null);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Table Number :");
        jPanel2.add(jLabel2);
        jLabel2.setBounds(16, 42, 120, 17);

        jcbTable.setFont(new java.awt.Font("Tahoma", 0, 14));
        jcbTable.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All Tables", "Table 1", "Table 2", "Table 3", "Table 4", "Table 5", "Table 6", "Table 7", "Table 8", "Table 9" }));
        jcbTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTableActionPerformed(evt);
            }
        });
        jPanel2.add(jcbTable);
        jcbTable.setBounds(140, 40, 100, 23);

        jbtnAddOrder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/ADD (3).png"))); // NOI18N
        jbtnAddOrder.setBorderPainted(false);
        jbtnAddOrder.setContentAreaFilled(false);
        jbtnAddOrder.setDefaultCapable(false);
        jbtnAddOrder.setFocusPainted(false);
        jbtnAddOrder.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/ADDMouseOver.png"))); // NOI18N
        jbtnAddOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAddOrderActionPerformed(evt);
            }
        });
        jPanel2.add(jbtnAddOrder);
        jbtnAddOrder.setBounds(280, 40, 100, 59);

        jtList.setFont(new java.awt.Font("Tahoma", 0, 14));
        jtList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "No.", "Menu Ordered", "Price", "Status"
            }
        ));
        jtList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtList);

        jPanel2.add(jScrollPane1);
        jScrollPane1.setBounds(16, 106, 588, 330);

        jbtnDeleteOrder.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/DELETE (2).png"))); // NOI18N
        jbtnDeleteOrder.setBorderPainted(false);
        jbtnDeleteOrder.setContentAreaFilled(false);
        jbtnDeleteOrder.setDefaultCapable(false);
        jbtnDeleteOrder.setFocusPainted(false);
        jbtnDeleteOrder.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/DELETEMouseOver.png"))); // NOI18N
        jbtnDeleteOrder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDeleteOrderActionPerformed(evt);
            }
        });
        jPanel2.add(jbtnDeleteOrder);
        jbtnDeleteOrder.setBounds(380, 40, 110, 60);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/REFRESH.png"))); // NOI18N
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.setDefaultCapable(false);
        jButton1.setFocusPainted(false);
        jButton1.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/REFRESHMouseOver.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);
        jButton1.setBounds(490, 40, 110, 60);

        add(jPanel2);
        jPanel2.setBounds(0, 0, 620, 460);
    }// </editor-fold>//GEN-END:initComponents

    private void jcbTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTableActionPerformed
        try {
            TableDetail();
        } catch (SQLException ex) {
            Logger.getLogger(Waiter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jcbTableActionPerformed

    private void jbtnAddOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAddOrderActionPerformed
        String str = null;

        if(jcbTable.getSelectedIndex() == 0)
        {
           JOptionPane.showMessageDialog(null, "Please select a table");
            new AddOrder(str).setVisible(false);
        }
        else if(jcbTable.getSelectedIndex() == 1){
            str = "1";
            new AddOrder(str).setVisible(true);
        }
        else if(jcbTable.getSelectedIndex() == 2){
            str = "2";
            new AddOrder(str).setVisible(true);
        }
        else if(jcbTable.getSelectedIndex() == 3){
            str = "3";
            new AddOrder(str).setVisible(true);
        }
        else if(jcbTable.getSelectedIndex() == 4){
            str = "4";
            new AddOrder(str).setVisible(true);
        }
        else if(jcbTable.getSelectedIndex() == 5){
            str = "5";
            new AddOrder(str).setVisible(true);
        }
        else if(jcbTable.getSelectedIndex() == 6){
            str = "6";
            new AddOrder(str).setVisible(true);
        }
        else if(jcbTable.getSelectedIndex() == 7){
            str = "7";
            new AddOrder(str).setVisible(true);
        }
        else if(jcbTable.getSelectedIndex() == 8){
            str = "8";
            new AddOrder(str).setVisible(true);
        }
        else if(jcbTable.getSelectedIndex() == 9){
            str = "9";
            new AddOrder(str).setVisible(true);
        }
        try {
            TableDetail();
        } catch (SQLException ex) {
            Logger.getLogger(Waiter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtnAddOrderActionPerformed

    private void jtListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtListMouseClicked
        clicked = true;
    }//GEN-LAST:event_jtListMouseClicked

    private void jbtnDeleteOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDeleteOrderActionPerformed
        if (clicked){
        String Table = (String) jtList.getValueAt(jtList.getSelectedRow(),0);
        String Menu = (String) jtList.getValueAt(jtList.getSelectedRow(),1);
    try {
        JOptionPane optionPane = new JOptionPane("Delete this " + Menu + " from table " + Table  + "?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
        JDialog dialog = optionPane.createDialog(null, "Delete Menu");
        dialog.setVisible(true);
        String y = optionPane.getValue().toString();
        int x = Integer.parseInt(y);
        if ( x == 0){
        deleteOrder(Table, Menu);
        JOptionPane.showMessageDialog(null, Menu + " has been deleted from table" + Table);
        TableDetail();
        }
        else
        {
        JOptionPane.showMessageDialog(null,"Data Not Deleted");
        }
        } catch (SQLException ex) {
           System.out.println("error");
        }
        }
        else{
            JOptionPane.showMessageDialog(null, "Please select something to delete");
        }
    }//GEN-LAST:event_jbtnDeleteOrderActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            TableDetail();
        } catch (SQLException ex) {
            Logger.getLogger(Waiter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               // try {
                    new Waiter().setVisible(true);
              //  } catch (SQLException ex) {
              //      Logger.getLogger(Waiter.class.getName()).log(Level.SEVERE, null, ex);
              //  }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnAddOrder;
    private javax.swing.JButton jbtnDeleteOrder;
    private javax.swing.JComboBox jcbTable;
    private javax.swing.JTable jtList;
    // End of variables declaration//GEN-END:variables

 public void TableDetail() throws SQLException{
     ResultSet rs;
     Main.getConnection();
     Statement stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

     String SQL = "SELECT TABLE_NUM AS TABLE_NUMBER, MENU_NAME AS MENU_NAME,"
             + "MENU_QTY AS MENU_QTY, KITCHEN_STATUS AS KITCHEN_STATUS "
             + "FROM ORDER_LIST ";

     if(jcbTable.getSelectedIndex() == 0)
     {
        SQL = SQL + "WHERE ";
     }
     else if(jcbTable.getSelectedIndex() == 1)
     {
        SQL = SQL + "where  table_num =  '1' and";
     }
     else if(jcbTable.getSelectedIndex()== 2)
     {
        SQL = SQL + "where  table_num =  '2' and";
     }
     else if(jcbTable.getSelectedIndex()==3)
     {
        SQL = SQL + "where  table_num = '3' and";
     }
     else if(jcbTable.getSelectedIndex()==4)
     {
        SQL = SQL + "where  table_num = '4' and";
     }
     else if(jcbTable.getSelectedIndex()==5)
     {
        SQL = SQL + "where  table_num = '5' and";
     }
     else if(jcbTable.getSelectedIndex()==6)
     {
        SQL = SQL + "where  table_num = '6' and";
     }
     else if(jcbTable.getSelectedIndex()==7)
     {
        SQL = SQL + "where  table_num = '7' and";
     }
     else if(jcbTable.getSelectedIndex()==8)
     {
        SQL = SQL + "where  table_num = '8' and";
     }
     else if(jcbTable.getSelectedIndex()==9)
     {
        SQL = SQL + "where  table_num = '9' and";
     }
     else
     {
        System.out.println("No such table!!!");
     }

     SQL = SQL + " BILL_STATUS = 'U' ORDER BY TABLE_NUM";

     rs = stmt.executeQuery(SQL);
     insertIntotable(rs);
 }

 private void insertIntotable(ResultSet rs)
 {
 jtList.removeAll();
        Vector columnNames = new Vector();
        Vector data = new Vector();

        try {
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();


            for (int i = 1; i <= columns; i++) {
                columnNames.addElement(md.getColumnName(i));
            }
           
            rs.first();
            if (rs.isFirst() == true && rs.isLast() == true) {
                // JOptionPane.showMessageDialog(null, "Hello");
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++) {
                    row.addElement(rs.getObject(i));

                }
 
                data.addElement(row);

                } else {
                //   rs.first();
                boolean loop = true;
                do {
                    //  rset.next();
                    Vector row = new Vector(columns);

                    for (int i = 1; i <= columns; i++) {
                      row.addElement(rs.getObject(i));
                    }

                    data.addElement(row);
                    if (rs.isLast()) {
                        loop = false;
                    } else {
                        rs.next();
                    }
                } while (loop);
            }

            } catch (SQLException e) {
            System.out.println("CHECK");
        }
        DefaultTableModel model = new DefaultTableModel(data, columnNames);

    jtList.setModel(model);
 }

 public void deleteOrder(String Table, String Menu) throws SQLException{
     ResultSet rs;
     Main.getConnection();
     Statement stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

     String SQL = "DELETE FROM ORDER_LIST WHERE TABLE_NUM = '"+Table+"' and menu_name = '"+Menu+"'";

     rs = stmt.executeQuery(SQL);
    }

    private AddOrder AddOrder(String str) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

}



